<?php


namespace app\api\model;


use app\common\library\dingding\AliDingDing;
use app\common\library\PlatformModel;
use app\common\model\BaseModel;
use common;
use think\Db;

class BikeWarnModel extends BaseModel
{
    protected $name = 'bike_warn';

    //添加预警记录
    function add($bike_info){
        $data = [
            'sn' => $bike_info['sn'],
            'city_id' => $bike_info['city_id'],
            'bike_id' => $bike_info['id'],
            'start_time' => date('Y-m-d H:i:s'),
            'state' => 0,
            'park_state' => $bike_info['park_state'],

        ];

        self::create($data);


        if ($data['park_state'] == BIKE_PARK_STAT_OSA){
            //al通知
            $city_info = (new PlatformModel())->get_city_al_token($data['city_id']);
            if ($city_info) {

                $logStr = null;
                switch ($data['park_state'])
                {
                    case BIKE_PARK_STAT_OSA:
                        $logStr = "【服务区外预警】车辆" . $bike_info['sn'] . "已出服务区3个小时，请尽快挪回车辆。";
                        break;
                    case BIKE_PARK_STAT_NPA:
                        $logStr = "【禁停区内预警】车辆" . $bike_info['sn'] . "已停留禁停区4小时，请及时挪入车站。";
                        break;
                    default:
                        $logStr = "【差车预警】车辆" . $bike_info['sn'] . "无人骑行超12小时，请及时挪入车站";
                        break;
                }

                $ding = new AliDingDing($city_info['al_token']);
                $ding->assign('text', $logStr)
                    ->at(true)
                    ->send();
            }
        }

    }

    //完成预警
    function complete_task($sn,$com_info){
        $info = $this->query_by_sn($sn);
        if ($info){
            $duration = time() - strtotime($info['start_time']);
            $data =[
                'state' => 100,
                'end_time' =>date('Y-m-d H:i:s'),
                'duration' =>$duration,
                'com_staff_type' => $com_info['com_staff_type'],
                'com_staff_id' => $com_info['com_staff_id'],
                'com_staff_name' => $com_info['com_staff_name']
            ];

            $where =[
                'id' => $info['id'],
                'state' => 0,
            ];

            self::update($data,$where);

            $data2 =[
                'is_warned' => 0
            ];

            $where2 =[
                'sn' =>$sn
            ];

            Db::table('tb_bike')->where($where2)->update($data2);
        }
    }

    //获取数据
    function query_by_sn($sn){
        $paramlist = [
            'id' =>'int',
            'sn' =>'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'start_time' =>'string',
            'state' => 'int',
        ];

        $sfs = common::array_keys($paramlist);
        $beans = [
            'sn'=>$sn,
            'state' => 0
        ];


        return $this->where($beans)->field($sfs)->find();
    }

    //统计
    function analyze($city,$city_name){
        $warn_list = $this->query_all_by_citys($city);
        if (count($warn_list) >0){
            $warn_osa = 0;
            $warn_npa = 0;
            $warn_default = 0;

            foreach ($warn_list as $row){
                switch ($row['park_state']) {
                    case BIKE_PARK_STAT_OSA:
                        $warn_osa++;
                        break;
                    case BIKE_PARK_STAT_NPA:
                        $warn_npa++;
                        break;
                    default:
                        $warn_default++;
                        break;
                }
            }

            //提示
            $city_info = (new PlatformModel())->get_city_al_token($city);
            if ($city_info){
                $dateH = date('H');
                $logStr = $city_name . "截止". $dateH . "点,共有" . $warn_default . "辆车无人骑行超12小时," . $warn_npa . "辆已停留禁停区4小时," . $warn_osa . "辆已出服务区3小时，请及时挪入车站。";
                $ding = new AliDingDing($city_info['al_token']);
                $ding->assign('text', $logStr)
                    ->at(true)
                    ->send();
            }
        }
    }

    function query_all_by_citys($city){
        $paramlist = [
            'id' =>'int',
            'park_state' => 'int'
        ];

        $sfs = common::array_keys($paramlist);

        $beans = [
            'city_id'=>$city,
            'state' => 0
        ];

        $rows = $this->where($beans)->field($sfs)->select();
        if ($rows->isEmpty()) {
            return [];
        }
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($paramlist, $row);
        }
        return $l;
    }
}